﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace LiMinCRMPlugin
{
    [Description("其他应付单保存前校验OA数据库更新金额")]
    [HotUpdate]
    public class OtherPaySaveServiceC:AbstractOperationServicePlugIn
    {

        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("FBILLTYPEID");//单据类型
            e.FieldKeys.Add("F_DEV_OAId");//OA唯一码
            e.FieldKeys.Add("F_DEV_DataSrc");//数据来源
        }
        public override void BeforeDoSaveExecute(BeforeDoSaveExecuteEventArgs e)
        {
            base.BeforeDoSaveExecute(e);
            var userId = this.Context.UserId;//当前用户ID
            if (userId.ToString() == "234781" || userId.ToString() == "16394")
            {
                DynamicObject[] dataEntitys = e.DataEntities;

                foreach (var dataEntity in dataEntitys)
                {
                    var value = (DynamicObject)dataEntity["BillTypeID"];
                    string FBILLTYPEID = value["Number"].ToString();//单据类型编码
                    if (FBILLTYPEID.EqualsIgnoreCase("QTYFD03_SYS") || FBILLTYPEID.EqualsIgnoreCase("QTYFD04_SYS"))
                    {
                        if (!dataEntity["F_DEV_OABILL"].ToString().IsNullOrEmpty() && dataEntity["F_DEV_OABILL"].ToString().EqualsIgnoreCase("话费报销单"))
                        {
                            DynamicObjectCollection FEntity = dataEntity["FEntity"] as DynamicObjectCollection;//明细信息              
                            foreach (DynamicObject entryRow in FEntity)//循环单据体遍历
                            {
                                int FSEQ = Int32.Parse(entryRow["Seq"].ToString());//行号
                                string F_DEV_OAEntryId = entryRow["F_DEV_OAEntryId"].ToString();//OA明细唯一码
                                string sql = string.Format(@"/*dialect*/ SELECT*FROM [CRM].[ecology].[dbo].[formtable_main_402_dt1] where id='" + F_DEV_OAEntryId + "'");
                                DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                                if (sql_toStirng.Rows.Count > 0)
                                {
                                    double bhsje = double.Parse(sql_toStirng.Rows[0]["bhsje"].ToString());//不含税金额
                                    double se = double.Parse(sql_toStirng.Rows[0]["se"].ToString());//税额
                                    double jshj = double.Parse(sql_toStirng.Rows[0]["jshj"].ToString());//价税合计
                                    double sbje = double.Parse(sql_toStirng.Rows[0]["sbje"].ToString());//实报金额
                                    entryRow["F_DEV_NOTAXAMOUNTFOR"] = bhsje;
                                    entryRow["F_DEV_TOTALAMOUNTFOR"] = jshj;


                                    if (sbje != jshj)
                                    {
                                        entryRow["NOTAXAMOUNTFOR"] = sbje - se;
                                        entryRow["TOTALAMOUNTFOR"] = sbje;
                                        //this.View.Model.SetValue("FNOTAXAMOUNTFOR", sbje - se, FSEQ - 1);
                                        //this.View.InvokeFieldUpdateService("FNOTAXAMOUNTFOR", FSEQ - 1);
                                        //this.View.Model.SetValue("FTOTALAMOUNTFOR", sbje, FSEQ - 1);
                                        //this.View.InvokeFieldUpdateService("FTOTALAMOUNTFOR", FSEQ - 1);
                                    }

                                }
                            }
                        }

                        if (!dataEntity["F_DEV_OABILL"].ToString().IsNullOrEmpty() && dataEntity["F_DEV_OABILL"].ToString().EqualsIgnoreCase("个人报销"))
                        {
                            DynamicObjectCollection FEntity = dataEntity["FEntity"] as DynamicObjectCollection;//明细信息              
                            foreach (DynamicObject entryRow in FEntity)//循环单据体遍历
                            {

                            }
                        }

                        if (!dataEntity["F_DEV_OABILL"].ToString().IsNullOrEmpty() && dataEntity["F_DEV_OABILL"].ToString().EqualsIgnoreCase("差旅报销"))
                        {
                            DynamicObjectCollection FEntity = dataEntity["FEntity"] as DynamicObjectCollection;//明细信息              
                            foreach (DynamicObject entryRow in FEntity)//循环单据体遍历
                            {
                                int FSEQ = Int32.Parse(entryRow["Seq"].ToString());//行号
                                string F_DEV_OAEntryId = entryRow["F_DEV_OAEntryId"].ToString();//OA明细唯一码
                                string sql = string.Format(@"/*dialect*/ SELECT*FROM [CRM].[ecology].[dbo].[formtable_main_274_dt1] where id='" + F_DEV_OAEntryId + "'");
                                DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                                if (sql_toStirng.Rows.Count > 0)
                                {
                                    double bhsje = double.Parse(sql_toStirng.Rows[0]["bhsje"].ToString());//不含税金额
                                    double se = double.Parse(sql_toStirng.Rows[0]["se"].ToString());//税额
                                    double jshj = double.Parse(sql_toStirng.Rows[0]["jshj"].ToString());//价税合计
                                    double sbje = double.Parse(sql_toStirng.Rows[0]["sbje"].ToString());//实报金额
                                    entryRow["F_DEV_NOTAXAMOUNTFOR"] = bhsje;
                                    entryRow["F_DEV_TOTALAMOUNTFOR"] = jshj;
                                    if (sbje != jshj)
                                    {
                                        entryRow["NOTAXAMOUNTFOR"] = sbje - se;
                                        entryRow["TOTALAMOUNTFOR"] = sbje;
                                        //this.View.Model.SetValue("FNOTAXAMOUNTFOR", sbje - se, FSEQ - 1);
                                        //this.View.InvokeFieldUpdateService("FNOTAXAMOUNTFOR", FSEQ - 1);
                                        //this.View.Model.SetValue("FTOTALAMOUNTFOR", sbje, FSEQ - 1);
                                        //this.View.InvokeFieldUpdateService("FTOTALAMOUNTFOR", FSEQ - 1);
                                    }

                                }
                            }
                        }

                    }
                }
            }
        }
    }
}
